package com.miao.activity.mq;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.miao.activity.entity.OrderInfoEntity;
import com.miao.activity.filter.UserFilter;
import com.miao.activity.service.OrderInfoService;
import com.miao.activity.vo.GOVo;
import com.miao.commonn.utils.R;
import com.miao.commonn.vo.GoodsVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.io.IOException;

/**
 * @author 武者
 * @title: Anliter
 * @projectName miaosha
 * @description:
 * @date 2022/3/2 20:00
 */
@Component
@RabbitListener(queues = "bbb")
public class Anliter {

    @Autowired
    UserFilter userFilter;

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    RedisTemplate redisTemplate;

    @RabbitHandler
    public void ss(String ss, Channel channel, Message message) throws IOException {
        GOVo goVo = JSONObject.parseObject(ss, new TypeReference<GOVo>() {
        });
        GoodsVo goodsVo = goVo.getGoodsVo();
        OrderInfoEntity orderInfoEntity = goVo.getOrderInfoEntity();
        if (goodsVo.getGoodsStock()==0){
            R update = userFilter.update(goodsVo);
        }
        BoundHashOperations goods_list = redisTemplate.boundHashOps("goods_list");
        goods_list.put(goodsVo.getGoodsId(),goodsVo);
        orderInfoService.save(orderInfoEntity);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
